home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga CD-ROM Collection
/
Amiga CD-ROM Collection - Auge 4000 and Cactus and Demo Util.iso
/
auge4000
/
15
/
wblander
/
source
/
rocket.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-12-02
|
21KB
|
1,035 lines
#include <exec/types.h>
#include <exec/devices.h>
#include <exec/memory.h>
#include <graphics/gfx.h>
#include <devices/gameport.h>
#include <devices/inputevent.h>
#include <intuition/intuition.h>
#include <graphics/sprite.h>
#include <devices/timer.h>
#include <hardware/custom.h>
#include <stdio.h>
#include <ctype.h>
#include "flame.h"
#include "sample.h"
#include "sounds.h"
#define INTUITION_REV 1
#define GRAPHICS_REV 1
#define LAYERS_REV 1
#define SAUCERSPEED 200
#define SAUCERDELAY 370
#define ROCKETHEIGHT 16
#define SPRWIDTH 32
#define PIXRATIO 2
#define SCALEFACTOR 100
#define XMAX (640*SCALEFACTOR)
#define YMAX (200*SCALEFACTOR)
#define XMIN (-(SPRWIDTH/PIXRATIO)*SCALEFACTOR)
#define YMIN (-ROCKETHEIGHT*SCALEFACTOR)
#define OK 0
#define LANDED 1
#define CRASHEDGOOD 2
#define CRASHEDBAD 3
#define CRASHEDSAUCER 4
#define MAINFLAME 4
#define LEFTFLAME 1
#define RIGHTFLAME 2
#define ALLFLAMES (MAINFLAME|LEFTFLAME|RIGHTFLAME)
USHORT rocket_image[ROCKETHEIGHT*2] = {
0x0000, 0x0000,
0x0000, 0x0000,
0x0100, 0x0100,
0x0100, 0x0100,
0x0380, 0x0380,
0x0380, 0x0380,
0x07c0, 0x07c0,
0x07c0, 0x07c0,
0x0fe0, 0x0fe0,
0x0fe0, 0x0fe0,
0x0820, 0x0820,
0x0820, 0x0820,
0x0820, 0x0820,
0x0000, 0x0000,
0x0000, 0x0000,
0x0000, 0x0000,
};
USHORT flame[3][ROCKETHEIGHT*2] = {
{
0x0000, 0x0000,
0x0000, 0x0000,
0x0100, 0x0100,
0x0100, 0x0100,
0x0380, 0x0380,
0x0380, 0x0380,
0x07c0, 0x07c0,
0x0fc0, 0x37c0,
0x0fe0, 0x0fe0,
0x0fe0, 0x0fe0,
0x0820, 0x0820,
0x0820, 0x0820,
0x0820, 0x0820,
0x0000, 0x0000,
0x0000, 0x0000,
0x0000, 0x0000,
},
{
0x0000, 0x0000,
0x0000, 0x0000,
0x0100, 0x0100,
0x0100, 0x0100,
0x0380, 0x0380,
0x0380, 0x0380,
0x07c0, 0x07c0,
0x07e0, 0x07d8,
0x0fe0, 0x0fe0,
0x0fe0, 0x0fe0,
0x0820, 0x0820,
0x0820, 0x0820,
0x0820, 0x0820,
0x0000, 0x0000,
0x0000, 0x0000,
0x0000, 0x0000,
},
{
0x0000, 0x0000,
0x0000, 0x0000,
0x0100, 0x0100,
0x0100, 0x0100,
0x0380, 0x0380,
0x0380, 0x0380,
0x07c0, 0x07c0,
0x07c0, 0x07c0,
0x0fe0, 0x0fe0,
0x0fe0, 0x0fe0,
0x0ba0, 0x0c60,
0x0920, 0x0ee0,
0x0920, 0x0aa0,
0x0000, 0x0380,
0x0000, 0x0100,
0x0000, 0x0100,
}
};
USHORT sprite_colors[3] = { 0x059f, 0x044d, 0x0bc0 };
#define SAUCERHEIGHT 5
USHORT saucer_image[3][SAUCERHEIGHT*2] = {
{
0x0ff0, 0x0ff0,
0xffff, 0xffff,
0xb6db, 0x6db6,
0xffff, 0xffff,
0x0ff0, 0x0ff0
},
{
0x0ff0, 0x0ff0,
0xffff, 0xffff,
0x6db6, 0xdb6d,
0xffff, 0xffff,
0x0ff0, 0x0ff0
},
{
0x0ff0, 0x0ff0,
0xffff, 0xffff,
0xdb6d, 0xb6db,
0xffff, 0xffff,
0x0ff0, 0x0ff0
}
};
UWORD *rbuf, *sbuf;
struct SimpleSprite rocket, saucer;
int rocket_sprite, saucer_sprite;
struct Window *window, *badwindow;
struct ViewPort *viewport;
struct Remember *Memory;
/* Intuition always wants to see these declarations */
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *LayersBase;
/* my window structure */
struct NewWindow NewWindow = {
200, 140,
300, 10,
0, 1,
CLOSEWINDOW | RAWKEY,
ACTIVATE | WINDOWCLOSE | SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH,
NULL,
NULL,
"Workbench Lander",
NULL,
NULL,
0, 0,
0, 0,
WBENCHSCREEN,
};
struct NewWindow BadWindow = {
200, 101,
300, 10,
0, 1,
CLOSEWINDOW,
WINDOWCLOSE | SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH,
NULL,
NULL,
"Don't land here...",
NULL,
NULL,
0, 0,
0, 0,
WBENCHSCREEN,
};
struct IntuiMessage *NewMessage;
struct IOAudio *DoneMessage;
extern Sound eagle1sound, eagle2sound;
struct InputEvent gameEvent;
struct MsgPort *gameMsgPort;
struct IOStdReq *gameIOMsg;
int ControllerType;
int GameportOpen;
int gameIOPending;
struct timerequest *tr;
/******************************************************/
/* Main Program */
/* */
/* This is the main body of the program. */
/******************************************************/
int x, y, vx, vy, ax, ay;
int buttons = 0;
char *cycle[] = {
"Workbench Lander",
"Use joystick to",
"Land on this window",
"Workbench Lander",
"Don't hit too hard",
"Don't fall over",
"Don't get caught",
"Workbench Lander",
"Space is big",
"Space is dark",
"It's hard to find",
"A place to park",
"BURMA SHAVE",
"Workbench Lander",
"Another fine hack",
"by Peter da Silva",
"with audio by ",
"Karl Lehenbauer",
"Workbench Lander",
"Pass it around",
"Call our board",
"Wintermute",
"(713) 933-2440",
"Or on usenet...",
"...!hoptoad!academ...",
"...!uhnix1!sugar...",
"...!{karl,peter}",
0
};
int cycle_position, cycle_timeout;
char *title[] = {
"Workbench Lander 2.1 \251 1987 Sugarland Software",
"Usenet: ...!hoptoad!academ!uhnix1!sugar!{karl,peter}"
};
int titmode;
display_cycle()
{
int newmode;
char *msg;
if(cycle[cycle_position][0]=='O' ||
cycle[cycle_position][0]=='(' ||
cycle[cycle_position][0]=='.')
newmode = 1;
else
newmode = 0;
if(newmode == titmode)
msg = -1;
else {
msg = title[newmode];
titmode = newmode;
}
SetWindowTitles(window, cycle[cycle_position], msg);
}
addflame(n)
int n;
{
if (!(buttons&n)) ThrustAudio(n);
buttons |= n;
recalc_rocket();
}
delflame(n)
int n;
{
if (buttons&n) ThrustQuiet(n);
buttons &= ~n;
recalc_rocket();
}
JoyMove(x, y)
int x, y;
{
static int lastx = 0, lasty = 0;
if(x<-1) x = -1;
if(x>1) x = 1;
if(y<-1) y = -1;
if(y>1) y = 1;
if(x!=lastx)
switch(x) {
case -1:
addflame(RIGHTFLAME);
ax = -10;
break;
case 0:
delflame(LEFTFLAME|RIGHTFLAME);
ax = 0;
break;
case 1:
addflame(LEFTFLAME);
ax = 10;
break;
}
if(y!=lasty)
if(y==-1) {
addflame(MAINFLAME);
ay = -20;
} else {
delflame(MAINFLAME);
ay = 10;
}
lastx = x;
lasty = y;
}
recalc_rocket()
{
int b, i;
rbuf[0] = rbuf[1] = 0;
for(i = 0; i < 2*ROCKETHEIGHT; i++)
rbuf[i+2] = rocket_image[i];
rbuf[2*ROCKETHEIGHT+2] = 0;
rbuf[2*ROCKETHEIGHT+3] = 0;
for(b = 0; b < 3; b++)
if(buttons & (1<<b))
for(i = 0; i < 2*ROCKETHEIGHT; i++)
rbuf[i+2] |= flame[b][i];
ChangeSprite(viewport, &rocket, rbuf);
}
set_saucer(n)
int n;
{
int i;
sbuf[0] = sbuf[1] = 0;
for(i = 0; i < 2*SAUCERHEIGHT; i++)
sbuf[i+2] = saucer_image[n][i];
sbuf[2*SAUCERHEIGHT+2] = 0;
sbuf[2*SAUCERHEIGHT+3] = 0;
ChangeSprite(viewport, &saucer, sbuf);
}
#define BEGIN_EXPLOSION 0
#define IN_EXPLOSION 1
#define END_EXPLOSION 2
int exploding;
int coloregs;
int counter;
int KeepGoing;
int timeout;
int saucer_timeout, saucer_x, saucer_y, saucer_cycle, saucer_up;
int autoflag;
int automode;
/* 'F' -- fall.
* 'G' -- go there.
* 'H' -- hover
* 'L' -- land it
* 'P' -- pause
*/
explode()
{
int i;
if(!exploding) {
SaucerQuiet();
CrashAudio();
counter = 21;
exploding = 1;
}
if(counter-- < 0) {
reinit();
for(i = 0; i < 3; i++)
SetRGB4(viewport, coloregs+1+i,
sprite_colors[i]&0x000F,
(sprite_colors[i]&0x00F0)>>4,
(sprite_colors[i]&0x0F00)>>8);
CrashQuiet();
} else {
for(i = 0; i < 3; i++)
SetRGB4(viewport, coloregs+1+i,
RangeRand(16),
RangeRand(16),
RangeRand(16));
}
}
reinit()
{
exploding = 0;
x = y = 200;
vx = 100;
vy = 0;
ax = 0;
ay = 10;
automode = 'F';
delflame(ALLFLAMES);
saucer_timeout = SAUCERDELAY;
timeout = 10;
}
int debug = 0;
main_cleanup()
{
if(saucer_sprite > 0)
FreeSprite(saucer_sprite);
if(rocket_sprite > 0)
FreeSprite(rocket_sprite);
FreeRemember(&Memory, TRUE);
if(badwindow)
CloseWindow(badwindow);
if(window)
CloseWindow(window);
if(GfxBase)
CloseLibrary(GfxBase);
if(LayersBase)
CloseLibrary(LayersBase);
if(IntuitionBase)
CloseLibrary(IntuitionBase);
}
main()
{
int i;
long flags;
Memory = NULL;
add_cleanup(main_cleanup, "main");
IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", INTUITION_REV);
if( IntuitionBase == NULL )
{
puts("can't open intuition\n");
cleanup(debug);
exit(20);
}
GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",GRAPHICS_REV);
if( GfxBase == NULL )
{
puts("can't open graphics library\n");
cleanup(debug);
exit(20);
}
LayersBase = OpenLibrary("layers.library", LAYERS_REV);
if (LayersBase == NULL)
{
puts("Can't open Layers Library\n");
cleanup(debug);
exit(20);
}
RocketAudioInit();
show_init();
if(( window = (struct Window *)OpenWindow(&NewWindow) ) == NULL)
{
puts("can't open window\n");
cleanup(debug);
exit(20);
}
if(( badwindow = (struct Window *)OpenWindow(&BadWindow) ) == NULL)
{
puts("can't open obstacle\n");
cleanup(debug);
exit(20);
}
viewport = ViewPortAddress(window);
if((rocket_sprite = GetSprite(&rocket, 2)) == -1)
if((rocket_sprite = GetSprite(&rocket, 4)) == -1)
if((rocket_sprite = GetSprite(&rocket, 6)) == -1) {
puts("can't get rocket_sprite\n");
cleanup(debug);
exit(20);
}
if((saucer_sprite = GetSprite(&saucer, rocket_sprite+1)) == -1) {
puts("can't get saucer_sprite\n");
cleanup(debug);
exit(20);
}
rocket.x = 0;
rocket.y = 0;
rocket.height = ROCKETHEIGHT;
saucer.x = 0;
saucer.y = 0;
saucer.height = SAUCERHEIGHT;
coloregs = ((rocket_sprite & 0x06)*2)+16;
for(i = 0; i < 3; i++)
SetRGB4(viewport, coloregs+1+i,
sprite_colors[i]&0x000F,
(sprite_colors[i]&0x00F0)>>4,
(sprite_colors[i]&0x0F00)>>8);
if(!(rbuf = AllocRemember(&Memory, 4L * ROCKETHEIGHT + 8L, MEMF_CHIP))) {
cleanup(debug);
exit(20);
}
if(!(sbuf = AllocRemember(&Memory, 4L * SAUCERHEIGHT + 8L, MEMF_CHIP))) {
cleanup(debug);
exit(20);
}
init_timer();
init_joy();
buttons = 0;
recalc_rocket();
set_saucer(0);
KeepGoing = TRUE;
timeout = 0;
cycle_timeout = 10;
cycle_position = 0;
autoflag = 1;
automode = 'F';
saucer_timeout = SAUCERDELAY;
saucer_up = 0;
MoveSprite(viewport, &saucer, -20, -20);
SetWindowTitles(window, -1, title[0]);
titmode = 0;
x = y = 200;
vx = 100;
vy = 0;
ax = 0;
ay = 10;
MoveSprite(viewport, &rocket, x/SCALEFACTOR, y/SCALEFACTOR);
while( KeepGoing )
{
flags = Wait((1 << window->UserPort->mp_SigBit) |
(1 << badwindow->UserPort->mp_SigBit) |
(1 << eagle1sound.port->mp_SigBit) |
(1 << eagle2sound.port->mp_SigBit) |
(1 << gameIOMsg->mn_ReplyPort->mp_SigBit) |
(1 << tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit));
WaitTOF();
if(flags & (1 << eagle1sound.port->mp_SigBit)) {
while(DoneMessage=(struct IOAudio *)GetMsg(eagle1sound.port) )
Eagle1Done();
ShowControl();
HideEagle();
}
if(flags & (1 << eagle2sound.port->mp_SigBit)) {
while(DoneMessage=(struct IOAudio *)GetMsg(eagle2sound.port) )
Eagle2Done();
if(autoflag && automode == 'P')
reinit();
HideControl();
}
if(flags & (1 << badwindow->UserPort->mp_SigBit))
while(NewMessage=(struct IntuiMessage *)GetMsg(badwindow->UserPort) )
{
int class = NewMessage->Class;
ReplyMsg( NewMessage );
switch( class )
{
case CLOSEWINDOW:
KeepGoing = FALSE;
break;
}
}
if(flags & (1 << gameIOMsg->mn_ReplyPort->mp_SigBit)) {
if(GetMsg(gameIOMsg->mn_ReplyPort)) {
JoyMove(gameEvent.ie_X, gameEvent.ie_Y);
SendGameIO();
}
}
if(flags & (1 << tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit)) {
if(GetMsg(tr->tr_node.io_Message.mn_ReplyPort)) {
if(exploding) {
explode();
} else {
if(timeout > 0) {
timeout--;
if(timeout == 0) {
SetWindowTitles(badwindow, BadWindow.Title, -1);
display_cycle();
cycle_timeout = 10;
}
} else if(timeout == 0) {
cycle_timeout--;
if(cycle_timeout <= 0) {
cycle_timeout = 10;
cycle_position++;
if(cycle[cycle_position]==0)
cycle_position = 0;
display_cycle();
}
}
if(saucer_timeout == 0) {
int dx, dy;
PlaySaucer();
if(saucer_up == 0) {
saucer_up = 1;
saucer_x = 200;
saucer_y = 200;
saucer_cycle = 0;
}
set_saucer(saucer_cycle++);
if(saucer_cycle >= 3)
saucer_cycle = 0;
dx = dy = 0;
if(saucer_x < x-SAUCERSPEED)
dx = SAUCERSPEED;
else if(saucer_x > x+SAUCERSPEED)
dx = -SAUCERSPEED;
if(saucer_y < y-SAUCERSPEED)
dy = SAUCERSPEED;
else if(saucer_y > y+SAUCERSPEED)
dy = -SAUCERSPEED;
if(dy == 0) dx += dx/2;
if(dx == 0) dy += dy/2;
saucer_x += dx;
saucer_y += dy;
MoveSprite(viewport, &saucer,
saucer_x/SCALEFACTOR, saucer_y/SCALEFACTOR);
} else {
if(saucer_timeout != -1)
saucer_timeout--;
if(saucer_up) {
saucer_up = 0;
MoveSprite(viewport, &saucer, -20, -20);
}
}
if(autoflag)
AutoPilot();
x += vx;
y += vy;
vx += ax;
vy += ay;
if(x > XMAX) x = XMIN;
if(x < XMIN) x = XMAX;
if(y < YMIN) y = YMAX;
if(y > YMAX) y = YMIN;
switch(HitWindow()) {
case OK:
if(ay==0) {
ay = 10;
timeout = 10;
saucer_timeout = SAUCERDELAY;
}
break;
case LANDED:
if(vy >= -2*SCALEFACTOR && vy < 0) {
timeout = 10;
saucer_timeout = SAUCERDELAY;
break;
}
if(vy >= 0 && vy <= SCALEFACTOR &&
vx >= -20 && vx <= 20
) {
if(timeout >= 0) {
if(vy <= 30 && vx == 0)
SetWindowTitles(window, "Great Landing!", -1);
else if(vy <= 70 && vx == 0)
SetWindowTitles(window, "Good Landing.", -1);
else
SetWindowTitles(window, "Just made it...", -1);
ShowEagle();
LandedAudio();
}
timeout = -1;
vx = vy = 0;
ax = ay = 0;
delflame(ALLFLAMES);
saucer_timeout = -1;
automode = 'P';
break;
}
case CRASHEDGOOD:
SetWindowTitles(window, "OUCH! That hurt!", -1);
explode();
break;
case CRASHEDBAD:
SetWindowTitles(badwindow, "OUCH! That hurt!", -1);
explode();
break;
case CRASHEDSAUCER:
SetWindowTitles(window, "GOTCHA!", -1);
explode();
break;
}
MoveSprite(viewport, &rocket,
x/SCALEFACTOR, y/SCALEFACTOR);
}
SendTimeIO();
}
}
if(flags & (1 << window->UserPort->mp_SigBit))
while( NewMessage=(struct IntuiMessage *)GetMsg(window->UserPort) )
{
int class = NewMessage->Class;
int code = NewMessage->Code;
ReplyMsg( NewMessage );
switch( class )
{
case CLOSEWINDOW:
KeepGoing = FALSE;
break;
case RAWKEY:
if(autoflag) {
autoflag = 0;
delflame(ALLFLAMES);
}
switch(code) {
case 0x4C:
addflame(MAINFLAME);
ay = -20;
break;
case 0x4C|0x80:
delflame(MAINFLAME);
ay = 10;
break;
case 0x4E:
addflame(LEFTFLAME);
ax = (buttons&RIGHTFLAME)?0:10;
break;
case 0x4E|0x80:
delflame(LEFTFLAME);
ax = (buttons&RIGHTFLAME)?-10:0;
break;
case 0x4F:
addflame(RIGHTFLAME);
ax = (buttons&LEFTFLAME)?0:-10;
break;
case 0x4F|0x80:
delflame(RIGHTFLAME);
ax = (buttons&LEFTFLAME)?10:0;
break;
}
break;
} /* end of switch (class) */
} /* end of while ( newmessage )*/
} /* end while ( keepgoing ) */
cleanup(debug);
exit(0);
} /* end of main */
AutoPilot()
{
static int lastwx = -1, lastwy = -1;
if(lastwx != window->LeftEdge || lastwy != window->TopEdge) {
if(lastwx != -1) {
timeout = 10;
saucer_timeout = SAUCERDELAY;
automode = 'G';
}
lastwx = window->LeftEdge;
lastwy = window->TopEdge;
}
switch(automode) {
case 'F': Fall(); break;
case 'G': GetThere(); break;
case 'H': Hover(); break;
case 'L': LandIt(); break;
case 'P': break;
default: break;
}
}
Fall()
{
if(y > (window->TopEdge / 2) * SCALEFACTOR)
automode = 'G';
setvxy(vx, vy);
}
GetThere()
{
int wantvx, wantvy;
int wanty = (window->TopEdge-ROCKETHEIGHT-2)*SCALEFACTOR;
int wantx = (window->LeftEdge+window->Width/2)*SCALEFACTOR;
if(x < wantx-10) wantvx = SCALEFACTOR;
else if(x > wantx+10) wantvx = -SCALEFACTOR;
else wantvx = 0;
if(y > wanty-10) wantvy = -SCALEFACTOR;
else if(y < wanty+10) wantvy = SCALEFACTOR;
else wantvy = 0;
if(x >= wantx-10*SCALEFACTOR && x <= wantx+10*SCALEFACTOR)
automode = 'H';
setvxy(wantvx, wantvy);
}
Hover()
{
if(vx <= 10 && vx >= -10 && vy <= 10 && vy >= -10)
automode = 'L';
setvxy(0, 0);
}
setvxy(wantvx, wantvy)
int wantvx, wantvy;
{
int wantax, wantay;
if(vx < wantvx-10) wantax = 10;
else if(vx > wantvx+10) wantax = -10;
else wantax = 0;
if(vy < wantvy-10) wantay = 10;
else if(vy > wantvy+10) wantay = -10;
else wantay = 0;
if(wantax != ax) {
switch(wantax) {
case -10: delflame(LEFTFLAME); addflame(RIGHTFLAME); break;
case 0: delflame(LEFTFLAME | RIGHTFLAME); break;
case 10: delflame(RIGHTFLAME); addflame(LEFTFLAME); break;
}
ax = wantax;
}
if(wantay != ay) {
switch(wantay) {
case -10: addflame(MAINFLAME); ay = -20; break;
case 0: break;
case 10: delflame(MAINFLAME); ay = 10; break;
}
}
}
LandIt()
{
setvxy(0, 50);
}
HitWindow()
{
int sx = x/SCALEFACTOR;
int sy = y/SCALEFACTOR;
int ret;
ret = checkwindow(sx, sy, window, 1);
if(!ret)
ret = checkwindow(sx, sy, badwindow, 0);
if(!ret)
ret = checksaucer();
if(ret)
automode = 'P';
return ret;
}
checkwindow(sx, sy, window, canland)
int sx, sy;
struct Window *window;
int canland;
{
int top = window->TopEdge-ROCKETHEIGHT+3;
int bottom = window->TopEdge+window->Height - 2;
int left = window->LeftEdge-SPRWIDTH+4*PIXRATIO;
int right = window->LeftEdge+window->Width-5*PIXRATIO;
if(sx<left || sx>right) return OK;
if(sy>bottom || sy<top) return OK;
if(canland) {
if(sy>top+3) return CRASHEDGOOD;
if(sx<left+7*PIXRATIO || sx>right-7*PIXRATIO) return CRASHEDGOOD;
return LANDED;
} else {
return CRASHEDBAD;
}
}
checksaucer()
{
int sx, sy, rx, ry, top, bottom, left, right;
if(!saucer_up) return OK;
rx = x/SCALEFACTOR;
ry = y/SCALEFACTOR;
sx = saucer_x/SCALEFACTOR;
sy = saucer_y/SCALEFACTOR;
top = sy-ROCKETHEIGHT+3;
bottom = sy+SAUCERHEIGHT - 2;
left = sx-SPRWIDTH+4*PIXRATIO;
right = sx+SPRWIDTH-5*PIXRATIO;
if(rx<left || rx>right) return OK;
if(ry>bottom || ry<top) return OK;
return CRASHEDSAUCER;
}
trash_timer()
{
AbortIO(tr);
DeleteTimer(tr);
}
init_timer()
{
if(!(tr = CreateTimer(UNIT_VBLANK))) {
printf("Can't create timer.\n");
cleanup(debug);
exit(20);
}
SendTimeIO();
add_cleanup(trash_timer, "timer");
}
SendTimeIO()
{
tr->io_Command = TR_ADDREQUEST;
tr->tr_time.tv_secs = 0;
tr->tr_time.tv_micro = 100000L;
SendIO(tr);
}
trash_joy()
{
if(gameIOPending) AbortIO(gameIOMsg);
if(ControllerType != GPCT_NOCONTROLLER)
SetControllerType(GPCT_NOCONTROLLER);
if(GameportOpen) CloseDevice(gameIOMsg);
if(gameIOMsg) DeleteStdIO(gameIOMsg);
if(gameMsgPort) DeletePort(gameMsgPort);
}
extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();
init_joy()
{
gameIOMsg = 0;
gameMsgPort = 0;
ControllerType = GPCT_NOCONTROLLER;
GameportOpen = 0;
gameIOPending = 0;
add_cleanup(trash_joy, "joystick");
if(!(gameMsgPort = CreatePort(0, 0))) {
printf("Can't create joystick port\n");
cleanup(debug);
exit(20);
}
if(!(gameIOMsg = CreateStdIO(gameMsgPort))) {
printf("Can't create joystick message.\n");
cleanup(debug);
exit(20);
}
if(OpenDevice("gameport.device", 1, gameIOMsg, 0) != 0) {
printf("Can't open gameport.device.\n");
cleanup(debug);
exit(20);
}
GameportOpen = 1;
if(SetControllerType(GPCT_ABSJOYSTICK) != 0) {
printf("Can't set controller type.\n");
cleanup(debug);
exit(20);
}
ControllerType = GPCT_ABSJOYSTICK;
if(SetControllerTrigger() != 0) {
printf("Can't set controller trigger.\n");
cleanup(debug);
exit(20);
}
SendGameIO();
}
SendGameIO()
{
gameIOMsg->io_Command = GPD_READEVENT;
gameIOMsg->io_Data = (APTR)&gameEvent;
gameIOMsg->io_Length = sizeof(gameEvent);
gameIOMsg->io_Flags = 0;
SendIO(gameIOMsg);
gameIOPending = 1;
}
SetControllerType(type)
SHORT type;
{
BYTE buffer[1];
gameIOMsg->io_Command = GPD_SETCTYPE;
gameIOMsg->io_Length = 1;
gameIOMsg->io_Data = (APTR)buffer;
buffer[0] = type;
SendIO(gameIOMsg);
WaitPort(gameMsgPort);
GetMsg(gameMsgPort);
return (int)gameIOMsg->io_Error;
}
SetControllerTrigger()
{
struct GamePortTrigger gpt;
gameIOMsg->io_Command = GPD_SETTRIGGER;
gameIOMsg->io_Length = sizeof(gpt);
gameIOMsg->io_Data = (APTR)&gpt;
gpt.gpt_Keys = GPTF_UPKEYS|GPTF_DOWNKEYS;
gpt.gpt_Timeout = 0;
gpt.gpt_XDelta = 1;
gpt.gpt_YDelta = 1;
return DoIO(gameIOMsg);
}